import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog, QTextEdit, QPushButton, QVBoxLayout, QWidget, QHBoxLayout, QLabel, QLineEdit, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import subprocess
import importlib

class DarlingManager(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.create_menu()
        
    def initUI(self):
        self.setWindowTitle("Darling运行器")
        self.setGeometry(100, 100, 800, 600)
        self.setWindowIcon(QIcon("icon.png"))
        
        self.install_action = QPushButton("安装Darling", self)
        self.install_action.clicked.connect(self.installDarling)
        
        self.open_action = QPushButton("打开Darwin命令行界面", self)
        self.open_action.clicked.connect(self.openDarwinCLI)

        self.run_action = QPushButton("运行Darling容器", self)
        self.run_action.clicked.connect(self.rundarling)


        self.script_layout = QHBoxLayout()
        self.script_label = QLabel('运行脚本:')
        self.script_input = QLineEdit()
        self.select_script_button = QPushButton('选择sh')
        self.select_script_button.clicked.connect(self.runScript)
        self.run_script_button = QPushButton("在容器上运行macOS脚本", self)
        self.run_script_button.clicked.connect(self.runScript)
        
        self.pkg_layout = QHBoxLayout()
        self.pkg_label = QLabel('安装pkg:')
        self.pkg_input = QLineEdit()
        self.select_pkg_button = QPushButton('选择pkg')
        self.select_pkg_button.clicked.connect(self.selectPkg)
        self.install_button = QPushButton('运行pkg')
        self.install_button.clicked.connect(self.installPkg)
        
        #self.dmg_layout = QHBoxLayout()
        #self.dmg_label = QLabel('安装dmg:')
        #self.dmg_input = QLineEdit()
        #self.select_dmg_button = QPushButton('选择dmg')
        #self.select_dmg_button.clicked.connect(self.selectPkg)
        #self.install_dmg_button = QPushButton('运行dmg')
        #self.install_dmg_button.clicked.connect(self.installPkg)

        self.output_text = QTextEdit(self)
        self.output_text.setReadOnly(True)

        self.output_label = QLabel('输出:')
        
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.install_action)
        self.layout.addWidget(self.run_action)
        self.layout.addWidget(self.open_action)
        self.layout.addWidget(self.script_label)
        self.layout.addWidget(self.script_input)
        self.layout.addWidget(self.select_script_button)
        self.layout.addWidget(self.run_script_button)
        self.layout.addWidget(self.pkg_label)
        self.layout.addWidget(self.pkg_input)
        self.layout.addWidget(self.select_pkg_button)
        self.layout.addWidget(self.install_button)
        #self.layout.addWidget(self.dmg_label)
        #self.layout.addWidget(self.dmg_input)
        #self.layout.addWidget(self.select_dmg_button)
        #self.layout.addWidget(self.install_dmg_button)
        self.layout.addWidget(self.output_label)
        self.layout.addWidget(self.output_text)
        #self.layout.addWidget(self.)
        
        self.central_widget = QWidget()
        self.central_widget.setLayout(self.layout)
        self.setCentralWidget(self.central_widget)

    def create_menu(self):
        menu_bar = self.menuBar()
        help_menu = menu_bar.addMenu("操作")
        install_menu = menu_bar.addMenu("安装")

        install_action = QAction("安装Darling", self)
        install_action.triggered.connect(self.installDarling)
        install_menu.addAction(install_action)

        open_action = QAction("打开Darling 容器Shell")
        open_action.triggered.connect(self.openDarwinCLI)

        about_action = QAction("关于", self)
        about_action.triggered.connect(self.show_about_dialog)
        help_menu.addAction(open_action)
        help_menu.addAction(about_action)
    def installDarling(self):
        self.output_text.append("安装Darling")
        
        os.system('deepin-terminal -e bash /opt/com.zhsoft.darling.runner/download.sh')

    def rundarling(self):
        os.system("darling")
        QMessageBox.warning(self, "成功！", "启动完成")
        

        
    def openDarwinCLI(self):
        self.output_text.append("打开Darwin命令行界面")
        os.system('deepin-terminal -e bash darling shell')
        
    def runScript(self):
        self.output_text.append("在容器上运行macOS脚本")
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.ExistingFile)
        file_dialog.setNameFilter("Shell Script Files (*.sh)")
        if file_dialog.exec_():
            selected_files = file_dialog.selectedFiles()
            self.sh_path = selected_files[0]
            self.script_input.setText(self.sh_path)
    def runsh(self):
        if not self.sh_path:
            QMessageBox.warning(self, "错误！", "请选择sh文件.")
            return
        self.output_text.append("在容器中运行sh:" + self.sh_path)
        process = subprocess.Popen(["deepin-terminal", "-e", "bash", "darling", "shell", self.sh_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        if stdout:
            self.output_text.append(stdout.decode("utf-8"))
        if stderr:
            self.output_text.append(stderr.decode("utf-8"))

    def show_about_dialog(self):
        QMessageBox.about(self, "关于", "一款在Linux上运行macOS应用的辅助工具\n通过darling容器来运行macOS应用\nCopyright ZhongHongSoftware")
        
    def selectPkg(self):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.ExistingFile)
        file_dialog.setNameFilter("PKG Files (*.pkg)")
        if file_dialog.exec_():
            selected_files = file_dialog.selectedFiles()
            self.pkg_path = selected_files[0]
            self.pkg_input.setText(self.pkg_path)
    def installPkg(self):
        if not self.pkg_path:
            QMessageBox.warning(self, "错误！", "请选择pkg文件.")
            return
        self.output_text.append("在容器中安装pkg：" + self.pkg_path)
        process = subprocess.Popen(["deepin-terminal", "-e", "bash", "installer", "-pkg", self.pkg_path, "-target", "/"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.output_text.append(self.pkg_path + "安装完成")
        QMessageBox.information(self, "提示！", "安装完成")
        stdout, stderr = process.communicate()
        if stdout:
            self.output_text.append(stdout.decode("utf-8"))
        if stderr:
            self.output_text.append(stderr.decode("utf-8"))

    def selectdmg(self):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.ExistingFile)
        file_dialog.setNameFilter("DMG Files (*.dmg)")
        if file_dialog.exec_():
            selected_files = file_dialog.selectedFiles()
            self.dmg_path = selected_files[0]
            self.dmg_input.setText(self.dmg_path)
    def installdmg(self):
        if not self.pkg_path:
            QMessageBox.warning(self, "错误！", "请选择dmg文件.")
            return
        self.output_text.append("在容器中安装dmg：" + self.dmg_path)
        process = subprocess.Popen(["daring", "install", self.dmg_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        if stdout:
            self.output_text.append(stdout.decode("utf-8"))
        if stderr:
            self.output_text.append(stderr.decode("utf-8"))

        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = DarlingManager()
    window.show()
    sys.exit(app.exec_())
